package com.zhaoguang.car.controller.shopmanager;


import com.zhaoguang.car.common.RespBean;
import com.zhaoguang.car.entity.CarImg;
import com.zhaoguang.car.entity.CarInfo;
import com.zhaoguang.car.mapper.ShopCarMapper;
import com.zhaoguang.car.service.ShopCarService;
import com.zhaoguang.car.vo.ListCarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/carshop")
public class ShopCarController {

    @Autowired
    ShopCarService shopCarService;

    @RequestMapping("/saveCar")
    public RespBean saveCar(CarInfo car) {
        int result = shopCarService.saveCar(car);
        if (result > 0) {
            return RespBean.success("录入汽车成功", car.getCarId());
        }

        return RespBean.success("录入汽车失败");
    }

    @RequestMapping("/deleteCarById")
    public RespBean deleteCarById(int carId) {
        int result = shopCarService.deleteCarById(carId);
        if (result > 0) {
            return RespBean.success("删除汽车成功");
        }
        return RespBean.success("删除汽车失败");
    }

    @RequestMapping("/updateCar")
    public RespBean updateCar(CarInfo car) {
        int result = shopCarService.updateCar(car);
        if (result > 0) {
            return RespBean.success("修改汽车成功");
        }
        return RespBean.success("修改汽车失败");

    }

    @RequestMapping("/findShopAllCarById")
    public RespBean findShopAllCarById(int shopId) {
        List<ListCarVo> result = shopCarService.findShopAllCarById(shopId);
        if (result != null) {
            return RespBean.success("查询成功", result);
        }
        return RespBean.success("查询失败");
    }

    @RequestMapping("/upLoadFile")
    public RespBean upLoadFile(String carId, MultipartFile file) {

        String str = file.getOriginalFilename();
        String strArr[] = str.split("\\.");
        // 设置图片名称
        String imgName = "car." + strArr[1];

        // *****保存图片到服务器上*****
        File folder = new File("/home/zhaoguang/project/car/car_img/" + carId);
//        File folder = new File("F:/迅雷下载/car/" + carId);
        if (!folder.isDirectory()) {
            folder.mkdirs();
        }
        try {
            File newFile = new File(folder.getAbsolutePath() + File.separator + imgName);
            System.out.println("上传到服务器的图片路径为：" + newFile.getAbsolutePath());
            file.transferTo(newFile);

            // *****保存图片URL到数据库的两张表中*****
            CarInfo car = new CarInfo();
            car.setCarId(Integer.parseInt(carId));
            String strImg = "http://112.124.20.153/car/car_img/" + carId + "/car." + strArr[1];
            car.setTitleImg(strImg);
            shopCarService.saveCarTitleImg(car);
            CarImg carImg = new CarImg();
            carImg.setCarId(Integer.parseInt(carId));
            carImg.setImgUrl(strImg);
            shopCarService.saveCarImg(carImg);

            System.out.println("文件上传成功");
            return RespBean.success("文件上传成功");
        } catch (IOException e) {
            e.printStackTrace();

            System.out.println("文件上传失败");
            return RespBean.success("文件上传失败");
        }
    }

    @RequestMapping("/upLoadFiles")
    public RespBean upLoadFiles(String carId, MultipartFile[] files) {
        System.out.println("上传多张图片：" + carId + " " + files);

        if (files != null) {
            int index = 1;

            //添加其他图片
            for (MultipartFile f : files) {
                String str = f.getOriginalFilename();
                String strArr[] = str.split("\\.");
                String imgName = "car_" + index + "." + strArr[1];

                // *****保存图片到服务器上*****
                File folder = new File("/home/zhaoguang/project/car/car_img/" + carId);
//                File folder = new File("F:/迅雷下载/Car/" + carId);
                if (!folder.isDirectory()) {
                    folder.mkdirs();
                }
                try {
                    File newFile = new File(folder.getAbsolutePath() + File.separator + imgName);
                    System.out.println("服务器中上传的文件路径：" + newFile.getAbsolutePath());
                    f.transferTo(newFile);

                    // *****保存图片URL到数据库中*****
                    CarImg img = new CarImg();
                    img.setCarId(Integer.parseInt(carId));
                    img.setImgUrl("http://112.124.20.153/car/car_img/" + carId + "/car_" + index + "." + strArr[1]);
                    shopCarService.saveCarImg(img);

                    System.out.println("图片 " + index + " 上传成功");
                    ++index;

                } catch (IOException e) {
                    e.printStackTrace();
                    return RespBean.success("文件上传失败");
                }
            }
            return RespBean.success("文件上传完成");
        }

        return RespBean.success("文件上传失败");
    }

}
